<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-input
        placeholder="请输入内容"
        class="input-with-select"
        v-model="queryInfo.query"
        clearable
        @clear="getUserList"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="getUserList"
        ></el-button>
      </el-input>
      <el-button
        type="primary"
        class="addUserButton"
        @click="dialogVisible = true"
        >添加用户</el-button
      >
      <!-- 点击添加用户按钮弹出的dialog对话框 -->
      <el-dialog
        title="添加用户"
        :visible.sync="dialogVisible"
        :before-close="handleClose"
      >
        <!-- 添加用户提示表单 -->
        <el-form
          :model="zhuceForm"
          :rules="zhuceRules"
          ref="zhuceForm"
          label-width="72px"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="zhuceForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="zhuceForm.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="zhuceForm.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="zhuceForm.mobile"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="transteMessage">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 点击修改按钮弹出对话框 -->
      <el-dialog
        title="修改信息"
        :visible.sync="editDialog"
        width="30%"
        :before-close="handleClose"
      
      >
        <!-- 对话框中内容 账号，邮箱，手机 -->
        <el-form
          :model="editMessage"
          :rules="zhuceRules"
          ref="zhuceForm"
          label-width="80px"
        >
          <el-form-item label="账号">
            <el-input v-model="editMessage.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="editMessage.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="editMessage.mobile"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialog = false">取 消</el-button>
          <el-button type="primary"  @click="editDialogUpdate"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 用户密码数据表格 -->
      <el-table :data="userLIst" border style="width: 100%" stripe>
        <el-table-column label="#" type="index" width="30"> </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="userStateChanged(scope.row.id)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip content="设置" placement="top" :enterable="false">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showDialog(scope.row.id)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top" :enterable="false">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteDialog(scope.row.id)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="分配角色" placement="top" :enterable="false">
            <el-button
              type="warning"
              icon="el-icon-s-tools"
              size="mini"
            ></el-button>
          </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格底部分页部分 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage1"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="this.queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totle"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 1,
      },
      userLIst: [],
      totle: 0,
      currentPage1: 1,
      dialogVisible: false,
      /*点击编辑按钮查询到的数据 */
      editMessage:{
        
      },
      zhuceForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      editDialog: false,
      zhuceRules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: "请输入邮箱 ", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getUserList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getUserList();
    },
    /* dialog对话框 */
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    /*dialog对话框点击确定后返回数据函数*/
    transteMessage() {
      this.$refs.zhuceForm.validate(async (valid) => {
        if (!valid) return;
        //校验成功添加用户
        const { data } = await this.$http.post("users", this.zhuceForm);
        if (data.meta.status != 201) {
          this.$message.error("添加用户失败");
        }
        this.$message.success("添加用户成功！");
        
        this.dialogVisible = false;
        this.getUserList();
      });
    },
    /*用showDialog得到点击编辑按钮那一栏的所有数据 并且打开编辑框 */
      async showDialog(id){
        const{data} = await this.$http.get('users/'+id) 
        
        if(data.meta.status!==200){
          return this.$message.error('查询失败')
        }
        this.editMessage = data.data
        this.editDialog = true;
      },

    async userStateChanged(userInfo) {
      const { data } = await this.$http.put(
        `users/${userInfo.id}/state/${userInfo.mg_state}`
      );
      if (data.meta.status !== 200) {
        userInfo.mg_state = !userInfo.mg_state;
        return this.$message.error("更新用户状态失败");
      }
      this.$message.success("更新成功");
    },
    /*点击编辑之后更改值*/
     editDialogUpdate(){
      this.$refs.zhuceForm.validate( async valid=>{
        if(!valid) return
       const{data}  = await this.$http.put('users/'+ this.editMessage.id,{email:this.editMessage.email,mobile:this.editMessage.mobile})
      if(data.meta.status !==200){
        return this.$message.error('更新用户信息失败！')
      }
      //关闭对话框
      this.editDialog = false
      this.getUserList()
      this.$message.success('用户信息更新成功')
      });
    }, 
    
    //删除选定用户列表
     async deleteDialog(id){
      const data = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(error=>{
          return error})
       console.log(data);
       if(data === 'cancel' ){
         this.$message.info("取消删除")
       }
       else {
        const {data} = await this.$http.delete('users/'+ id)
        this.getUserList()
      
       }
    },
    async getUserList() {
      const { data } = await this.$http.get("users", {
        params: this.queryInfo,
      });
      if (data.meta.status !== 200) {
        return this.$message.error("获取用户数据失败！");
      }
      this.userLIst = data.data.users;
      this.totle = data.data.total;
    },
  },
};
</script>

<style lang="less" scoped>
.box-card {
  margin-top: 15px;
}
.input-with-select {
  width: 430px;
}
.addUserButton {
  margin-left: 30px;
}
</style>